package com.sooncode.project.focusservice.controller;

import com.alibaba.fastjson.JSON;
import com.sooncode.project.core.finder.Page;
import com.sooncode.project.focusservice._.base.Utils;
import com.sooncode.project.focusservice.model.Course;
import com.sooncode.project.focusservice.model.CourseFile;
import com.sooncode.project.focusservice.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    CourseService service;
    @PostMapping("course")
    public boolean add(@RequestParam Map map){
        if(map.containsKey("measure"))
            map.put("measure",map.get("measure").toString().split(","));
        map.put("files", JSON.parseArray(map.get("files").toString(), CourseFile.class));
        return service.add(map);
    }
    @PutMapping("course")
    public boolean modify(@RequestParam String id,@RequestParam Map map){
        if(map.containsKey("measure"))
            map.put("measure",map.get("measure").toString().split(","));
        map.put("files", JSON.parseArray(map.get("files").toString(), CourseFile.class));
        return service.modify(id,map);
    }

    @DeleteMapping("course")
    public boolean delete(@RequestParam String[] ids){
        for(String id:ids){
            service.delete(id);
        }
        return true;
    }
    @GetMapping("courses")
    public Page<Course> getPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return service.getPageList(pageSize,pageIndex-1,map);
    }
    @GetMapping("courseList")
    public List<Course> getCourseList(){
        return service.getCourseList();
    }
}
